/* This file is part of the Pardus Economy Planner.

   The Pardus Economy Planner is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation, either version 3 of
   the License, or (at your option) any later version.

   The Pardus Economy Planner is distributed in the hope that it
   will be useful, but WITHOUT ANY WARRANTY; without even the implied
   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   See the GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the Pardus Economy Planner.  If not, see
   <http://www.gnu.org/licenses/>.
*/

Panels.Main.RenderImage = function(id, image)
{
  var tile = $("nav_" + id);
  tile.empty();

  if (!tile)
    return;

  if (image == "transparent.png")
    void(0); // Nothing to do here

  else
    tile.adopt(new Element("img", { "src": Utility.Config.Get("Imagepack") + "/" + image, "height": "32px", "width": "32px" }));
}

Panels.Main.Add = function(objectType, id, x, y, type, level, name, owner) // Creates and adds it to the screen.
{
  switch (objectType)
  {
    case eBuilding:
      var object = new Building(id, x, y, name, owner, level);

      if ($H(object.Production()).getKeys().length == 0)
        object.SetLevel(1);
      break;

    case ePlanet:
      var object = new Planet(id, x, y, name, level);
      break;

    case eStarbase:
      var object = new Starbase(id, x, y, type, name == "npc", owner, level);
      break;

    case eBackground: // FIXME: Add background
      break;

    default:
      return;
  }

  Utility.Object.Add(object);
  Utility.Tile.DeSelect(id);
  Panels.Main.RenderImage(id, "foregrounds/" + object.Info().Image);
  return object;
}

Panels.Main.Create = function(object, id, x, y, type, level, name, owner) // Called when an object is created.
{
  var info = Panels.Main.Add(object, id, x, y, type, level, name, owner).Info();

  if ($("useSelectedTiles").checked)
  {
    var selected = Utility.Tile.GetSelected();

    for (var i = 0; i < selected.length; i++)
    {
      var tile = selected[i];
      if (Utility.Object.Exists(tile.ID) || !(tile.Type & info.Tile)) // Object there, skip.
        continue;

      Panels.Main.Add(object, tile.ID, tile.X, tile.Y, type, level, name, owner);
    }
  }

  HideMenus();
}

Panels.Main.SetLevel = function(object, id, level)
{
  var data = Utility.Object.Get(id, object);

  if (object == eBuilding)
    data.SetLevel(level);
  else
    data.SetPopulation(level);
    
  Utility.Object.Update(data);

  Panels.Main.ShowInfo(object, id);
}

Panels.Main.DeleteObject = function(object, id)
{
  Utility.Object.Delete(id, object);
  Panels.Main.RenderImage(id, "transparent.png");
  HideMenus();
}

Panels.Main.DeleteAll = function(objectType, id)
{
  if (!confirm("Are you sure you want to delete all?"))
    return;

  var object = Utility.Object.Get(id, objectType);
  var objects = Utility.Object.GetByType(object);

  for (var i = 0; i < objects.length; i++)
  {
    Panels.Main.DeleteObject(objectType, objects[i].ID);
  }
}

Panels.Main.Render = function()
{
  var sector = Global.DisplayedSector;

  $("MapArea").set("html", sector.ToHtml());

  var buildings = sector.Buildings;
  var planets = sector.Planets;
  var starbases = sector.Starbases;
  var backgrounds = sector.Backgrounds;

  for (var i = 0; i < buildings.length; i++)
    Panels.Main.RenderImage(buildings[i].ID, "foregrounds/" + buildings[i].Info().Image);

  for (var i = 0; i < planets.length; i++)
    Panels.Main.RenderImage(planets[i].ID, "foregrounds/" + planets[i].Info().Image);

  for (var i = 0; i < starbases.length; i++)
    Panels.Main.RenderImage(starbases[i].ID, "foregrounds/" + starbases[i].Info().Image);

  for (var i = 0; i < backgrounds.length; i++)
    Panels.Main.RenderImage(backgrounds[i].ID, backgrounds[i].Image);
}